﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MvcEngine.Core.Repositories;

using MvcEngine.Core;
using System.Data.Linq;
using MvcEngine.Core.Website;
using MvcEngine.Core.Helpers;
using MvcEngine.Core.Base;

namespace MvcEngine.LinqToSqlProvider
{
    public class SiteRepository : Repository, ISiteRepository
    {
        public Site GetSiteByDomain(string sitedomain)
        {
            using (var contextManager = CreateContextManager(true))
            {
                return (from s in contextManager.Context.Sites()
                        join d in contextManager.Context.SiteDomains() on s.Id equals d.SiteId
                        where d.Name == sitedomain
                        select s).SingleOrDefault();
            }
        }

        public Site Get(Guid id)
        {
            using (var contextManager = CreateContextManager(true))
            {
                return contextManager.Context.Sites().SingleOrDefault(s => s.Id == id);
            }
        }

        public IList<Site> GetAll()
        {
            using (var contextManager = CreateContextManager(true))
            {
                return contextManager.Context.Sites().ToList();
            }
        }

        public void Update(Site site)
        {
            using (var contextManager = CreateContextManager(false))
            {
                contextManager.Commit();
            }
        }

        public void Add(Site site)
        {
            using (var contextManager = CreateContextManager(false))
            {
                contextManager.Context.Sites().InsertOnSubmit(site);
                contextManager.Commit();
            }
        }
    }
}
